/**
 * 
 */
package com.tza.web.t_net.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author tzammel
 */
public class LoginForm extends Composite implements HasText
{
   ActionContext                    actionContext;
   private static LoginFormUiBinder uiBinder = GWT.create(LoginFormUiBinder.class);
   @UiField
   TextBox                          textBox;
   @UiField
   PasswordTextBox                  passwdBox;
   @UiField
   Button                           LoginButton;

   interface LoginFormUiBinder extends UiBinder<Widget, LoginForm>
   {
   }

   /**
    * Because this class has a default constructor, it can be used as a binder
    * template. In other words, it can be used in other *.ui.xml files as
    * follows: <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    * xmlns:g="urn:import:**user's package**">
    * <g:**UserClassName**>Hello!</g:**UserClassName> </ui:UiBinder> Note that
    * depending on the widget that is used, it may be necessary to implement
    * HasHTML instead of HasText.
    */
   public LoginForm()
   {

      // Wrap the content in a DecoratorPanel

      initWidget(uiBinder.createAndBindUi(this));
   }

   public LoginForm(ActionContext mainActionContext)
   {
      initWidget(uiBinder.createAndBindUi(this));

      // Can access @UiField after calling createAndBindUi
      actionContext = mainActionContext;
   }

   public void setText(String text)
   {

   }

   /**
    * Gets invoked when the default constructor is called and a string is
    * provided in the ui.xml file.
    */
   public String getText()
   {
      return "";
   }

   @UiHandler("LoginButton")
   void onLoginButtonClick(ClickEvent event)
   {
      if ("tawfiq".equals(textBox.getText()) && "zed".equals(passwdBox.getText()))
      {

         actionContext.getMainEventBus().fireEvent(new AuthenticationEvent());
         this.removeFromParent();
      }
   }
}
